package com.hshx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hshx.VO.UsersVo;
import com.hshx.dao.DeptDao;
import com.hshx.dao.RoleDao;
import com.hshx.dao.UsersDao;
import com.hshx.pojo.Users;
import com.hshx.service.DeptService;
import com.hshx.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

//@Transactional
@Service
public class UsersServiceImpl extends ServiceImpl<UsersDao, Users> implements UsersService {
    @Autowired
    private UsersDao usersDao;
    @Resource
    private RoleDao roleDao;
    @Resource
    private DeptDao deptDao;


    @Override
    public List<UsersVo> getAllUsersVo() {
        List<Users> usersList=usersDao.selectList(new QueryWrapper<Users>());
        if(usersList==null||usersList.size()==0){
            return null;
        }
        List<UsersVo> usersVoList=new ArrayList<>();
        usersList.forEach(u-> {
            UsersVo uv=new UsersVo();
            uv.setUsers(u);
            uv.setRole(roleDao.selectById(u.getUsers_role()));
            uv.setDept(deptDao.selectById(u.getUsers_dept()));
            usersVoList.add(uv);
        });
        return usersVoList;
    }

    @Override
    public UsersVo getUsersVo(int users_id) {
        UsersVo uv=null;
        Users users = usersDao.selectById(users_id);
        if(users==null||users.getUsers_id()==0){
            return uv;
        }else{
            uv=new UsersVo();
        }
        uv.setUsers(users);
        uv.setRole(roleDao.selectById(users.getUsers_role()));
        uv.setDept(deptDao.selectById(users.getUsers_dept()));
        return uv;
    }
}
